﻿<?php
class user_register_process{
	
	private $db ;
	private $request ;
	private $db_obj_user ; 

	function __construct( $db , $request ){

		$this->db = $db ;
		$this->request = $request ;

		$this->db_obj_user = new db_obj_user( $db );
	}
	
	function do_register(){
		
		if ( !$this->request || !$this->db ){
			return false ;
		}

		$username = $this->request['username'] ;
		$passwd = $this->request['passwd'];
		$role = '0' ;
		$state = 'offline' ;

		$register_time = date("Y-m-d");
		$last_login_time = date("Y-m-d H-i-s",time()+8*60*60);
		$last_login_ipadd = $_SERVER['REMOTE_ADDR'];

		$sql = "INSERT INTO test_user SET 
				username     ='$username' , 
				passwd   = md5('$passwd') ,
				role     ='$role',
				state    ='$state',
				register_time = '$register_time',
				last_login_time = '$last_login_time',
				last_login_ipadd = '$last_login_ipadd'";

		$res = $this->db->query( $sql ) ;
	 
		if (PEAR::isError($res)) {

			return false ;
		}

		return true ;
	}

	function post_register(){
		
		//建立相应用户的user_profile表中的一行记录
		$sql = "INSERT INTO test_user_profile SET name = '$this->name'" ;

		$res = $this->db->query( $sql ) ;
		if (PEAR::isError($res)) {

			return false ;
		}

		return true ;
	}

	static function delete_user( $db , $username ){
				
		$sql = "DELETE FROM test_user WHERE username = '$username'";

		$res = $db->query( $sql ) ;
		if (PEAR::isError($res)) {

			return false ;
		}
		return true ;
	}
}
